package com.example.ymm.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.*;

import java.util.List;

@Configuration
public class WebAppConfigurer implements WebMvcConfigurer  {

//	@Autowired
//	private CorsInterceptor corsInterceptor;

	@Autowired
	IgnoreUrlsConfig ignoreUrlsConfig;
	/**
	 * 配置拦截器
	 */
	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		// TODO Auto-generated method stub
		// 多个拦截器组成一个拦截器链
		// addPathPatterns 用于添加拦截规则
		// excludePathPatterns 用户排除拦截

	//	registry.addInterceptor(corsInterceptor).addPathPatterns("/**"); // 对所有路径应用跨域设置
		List<String> urls = ignoreUrlsConfig.getUrls();

//		String[] objects = urls.toArray(new String[urls.size()]);
		registry.addInterceptor(new RateLimiterInterceptor())
				.addPathPatterns("/**")
				.excludePathPatterns(urls);
//		registry.addInterceptor(new AuthorInterceptor())
//				.addPathPatterns("/**")
//				.excludePathPatterns(urls);
	}

	/**
	 * 添加自定义的静态资源映射 这里使用代码的方式自定义目录映射，并不影响Spring Boot的默认映射，可以同时使用。
	 */
	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		registry.addResourceHandler("/**").addResourceLocations("/static/","/");
	}



}